/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameListPanel.java
 *
 * Created on Sep 16, 2011, 6:37:25 PM
 */

package com.agentslab.gameclient;

import aa.core.ActorMessage;
import aa.core.Contract;
import aa.core.Platform;
import com.agentslab.constants.APIConstants;
import javax.swing.JFrame;
import javax.swing.JList;

/**
 *
 * @author mehadihasan
 */
public class GameListPanel extends javax.swing.JPanel {

    /** Creates new form GameListPanel */
    public GameListPanel() {
        this.platform = APIConstants.platform;
        initComponents();
    }

    public JList getGameList() {
        return gameList;
    }

    public void setGameList(JList gameList) {
        this.gameList = gameList;
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        gameList = new javax.swing.JList();
        joinButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();

        jLabel1.setText("Please press the following button to search for games then select one to join.");

        jScrollPane1.setViewportView(gameList);

        joinButton.setText("Join");
        joinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinButtonActionPerformed(evt);
            }
        });

        searchButton.setText("Search Games");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(searchButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(433, Short.MAX_VALUE)
                        .add(joinButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(23, 23, 23)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                .add(searchButton)
                .add(7, 7, 7)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(joinButton)
                .add(29, 29, 29))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:

        Object[] argsreg=new Object[3];
        argsreg[0] = new Contract(1000000,1000,1);
        argsreg[1] = APIConstants.guiActor;
        argsreg[2] = ""+System.currentTimeMillis();

        ActorMessage am_reg = new ActorMessage(platform.getActorNameOfPlatform(),
                                               APIConstants.gameBroker,
                                               "getGameList",
                                               argsreg,
                                               false
                                               );

        platform.sendMessage(am_reg);
    }//GEN-LAST:event_searchButtonActionPerformed

    private void joinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_joinButtonActionPerformed
        // TODO add your handling code here:

        // the following code must be uncommented later.
//        Object[] argsreg=new Object[2];
//        argsreg[0] = this.getGameList().getSelectedValue();
//        argsreg[1] = ""+System.currentTimeMillis();
//
//        ActorMessage am_reg = new ActorMessage(platform.getActorNameOfPlatform(),
//                                               APIConstants.guiActor,
//                                               "updateServerList",
//                                               argsreg,
//                                               false
//                                               );
//
//        platform.sendMessage(am_reg);

        //the following code is used for quick start must be commented later.
        Object[] argsreg= new Object[3];
        argsreg[0]= "";
        argsreg[1] = "";
        argsreg[2] = ""+System.currentTimeMillis();

        ActorMessage am_reg = new ActorMessage(APIConstants.platform.getActorNameOfPlatform(),
                                               APIConstants.guiActor,
                                               "startClient",
                                               argsreg,
                                               false
                                               );

        APIConstants.platform.sendMessage(am_reg);

    }//GEN-LAST:event_joinButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList gameList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton joinButton;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
    private final Platform platform;
}
